package com.woniu.zsd.platform.service.impl;

import com.woniu.zsd.platform.model.ChatMessage;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class ChatMessageService {

    @Autowired
    private RedisTemplate redisTemplate;

    private static final String CHAT_CHANNEL_PREFIX = "chat_channel:";

    // 存储聊天消息
    public void saveMessage(ChatMessage chatMessage) {
        String key = CHAT_CHANNEL_PREFIX + chatMessage.getReceiver();
        redisTemplate.opsForList().rightPush(key, chatMessage);
    }

    // 获取聊天记录
    public List<ChatMessage> getChatHistory(String userId) {
        String key = CHAT_CHANNEL_PREFIX + userId;
        return redisTemplate.opsForList().range(key, 0, -1);
    }
}
